package com.xgo.http.iwo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.untils.Common;
import com.xgo.http.XinxinCrawler;
/* http://http://www.aivergems.com   */
public class IwoCrawler {
	private static int index = 1;
	private static int max = 13;
	private static String path = "d:/iwo/"; 
	private static String baseLink = "http://www.aivergems.com/awhty/ShowClass.asp?ClassID=20&page="; 
	private static Logger logger = LoggerFactory.getLogger(XinxinCrawler.class);
	public static void main(String[] args) {
		IwoCrawler crawler = new IwoCrawler();
		crawler.run(baseLink + index);
	}
	
	public void run(String szUrl){
		if(index <= max){
			//下载页面
			Document doc = null;
			try {
				System.out.println(szUrl);
				URL url = new URL(szUrl);
				doc = Jsoup.parse(url, 60000);
			} catch (MalformedURLException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			if(doc == null ){
				logger.error(" muju html down load failed.");
				return;
			}
			try {
				parseShop(doc);
			} catch (IOException e1) {
				logger.error(e1.getMessage());
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			index++;
			run(baseLink + index);
		
		}
	}

	private void parseShop(Document doc) throws IOException{
		//获取页面商品
		Elements lis = doc.getElementsByAttributeValue("class", "list_main").get(0).getElementsByTag("li");
		if(lis != null && lis.size() > 0){
			for (Element element : lis) {
				Elements as = element.getElementsByTag("a");
				String url = as.get(0).getElementsByTag("img").attr("src");
				String title = as.get(0).attr("title");
				
				int fileFormatBegin = url.lastIndexOf(".");
				String fileFormat = url.substring(fileFormatBegin);
				try {
					url = "http://www.aivergems.com/" + url;
					Common.download(url, path + title + fileFormat);
				} catch (IOException e) {
					throw new IOException("download image error.:" + url);
				}
			}
		}
	}
}
